from utils.handle_path import log_path
import logging,json
from logging import handlers

def handle_log():
    app=logging.getLogger(name="app")

    pycharm = logging.StreamHandler()
    file = handlers.TimedRotatingFileHandler(filename=log_path,when='D',encoding='utf-8',interval=1, backupCount=5)

    fmt =  '%(asctime)s-%(name)s-%(levelname)s-%(funcName)s-[line:%(lineno)d]：%(message)s'
    log_fmt = logging.Formatter(fmt=fmt)

    app.setLevel(level=logging.DEBUG)

    pycharm.setFormatter(fmt=log_fmt)
    file.setFormatter(fmt=log_fmt)

    app.addHandler(pycharm)
    app.addHandler(file)
    return app

log = handle_log()

def log_tojson(message):
    json_message=json.dumps(message,ensure_ascii=False,indent=2)
    return json_message